<link rel="stylesheet" href="./css/push.css" />
<div class="comm-top">
	<div class="title-left">
		高级群发
        <span style="font-size:12px;color:#aaa;font-weight:normal;">（订阅号一天只可以群发一条信息，<b style="color:red;">素材来源于关键词回复</b>）</span>
	</div>
	<div class="title-right">
    <form class="navbar-form navbar-right" role="search" style="margin-top:2px;">
        <a href='javascript:void(0)' class='c-green' style="margin-right:30px">选择新素材【+】</a>
            选择群发分组
            <select class="textarea" id="qunfa_group" style="border-radius:3px;">
                <option value="-1">全部分组</option>
            </select>&nbsp;&nbsp;&nbsp;
            选择消息类型
            <select class="textarea" style="width:110px;height:30px;font-size:13px;border-radius:3px;" onchange="location.href='push.php?type='+$(this).val();">
                <option value="1">文字</option>
                <option value="2">单图文</option>
                <option value="3">多图文</option>
            </select>
    </form>
	</div>
</div>
<div class="list-table">
	<div class="wrapper" style="overflow:visible;">
		<table class="stable">
        <thead>
            <tr>
                <th>关键词</th>
                <th>推送<{if $smarty.get.type==2 || $smarty.get.type==3}>标题<{else}>内容<{/if}></th>
                <th>状态</th>
                <th>操作 </th>
            </tr>
        </thead>
        <tbody>
            <{if $infoList}>
                <{foreach item=row from=$infoList}>
                <tr mid='<{$row.id}>'>
                    <td><strong class="c-red"><{$row.keyword}></strong></td>
                    <td><{$row.info_title|default:$row.info_intro}></td>
                    <td><{if $row.is_send==1}><div style='color:#009997;font-weight:bold;'>已推送</div><{else}>未推送<{/if}></td>
                    <td>    
                        <{if $smarty.get.type==2}>
                            <a href="../info/single.php?a=add&id=<{$row.id}>" title="编辑">
                        <{elseif $smarty.get.type==3}>
                            <a href="../info/multi.php?a=edit&id=<{$row.id}>" title="编辑">
                        <{else}>
                            <a href="../info/index.php?a=add&id=<{$row.id}>" title="编辑">
                        <{/if}>
                        <i class="glyphicon glyphicon-edit"></i></a>
                        <a href="javascript:void(0)" title="删除"><i class="glyphicon glyphicon-trash" onclick="delRow(<{$row.id}>)"></i></a>
                        <a style='cursor:pointer;font-size:12px;' id="sendAllBtn<{$row.id}>" onclick='send(<{$row.id}>,this)'>群发</a>
                    </td>
                </tr>
                <{/foreach}>
            <{else}>
                <tr>
                    <td colspan="5">
                        <p class="c-gray wrap-null" style="cursor:pointer;text-align:center;padding:10% 2% 10% 2%;"> 暂无可用素材！->
                        <span class="c-green" style="color:#009997;">“点此选择” </span> 吧！ </p>
                    </td>
                </tr>
            <{/if}>
        </tbody>
        </table>
    </div>
</div>

<div id="TB_overlayBG"></div>
<div class="box">
	<div class="close_pop"><img src="../public/img/close_pop.png"></div>
	<table class="table table-library" style="border-top:1px solid #ddd; margin-bottom:0;">
		<thead>
			<tr>
				<th class="t1">关键词</th>
				<th class="t2">回复内容</th>
				<th class="t5">操作 </th>
			</tr>
		</thead>  
		<tbody style="background-color:#fff;">

		</tbody>
	</table>
</div>
<script type="text/javascript">
var type = parseInt(<{$smarty.get.type|default:1}>);
//页面加载完后加载分组
$(function(){
    $.post('push.php',{action:'get_group'},function(res){
        var optionStr='';
        $.each(res,function(i,n){
            optionStr+='<option value="'+n.id+'">'+n.name+'</option>';
        })
        $('#qunfa_group').append(optionStr);
    },'json');
});

var controllerPath = 'mass_common';
var finalId = '';
$('select:last').val(type);
	
$(document).ready(function() {
    $(".c-green").click(function(){
        //选择素材
        $.post('mass_common.php',{act:'getMsg',type:type},function(res){
            if(res=='empty'){
                var addphp = 'text';
                if(type==2){
                    addphp = 'single';
                }else if(type==3){
                    addphp = 'multi';
                }
                var htmlStr = "<tr><td colspan='3'>无内容，<a href='../info/"+addphp+".php' style='font-size:11px;color:#009997'>点此添加</a></td></tr>"
                $('tbody:last').html(htmlStr);
                return ;
            }
            
            var htmlStr = '';
            $.each(res,function(i,val){
                htmlStr += '<tr><td><strong class="c-red">'+val['keyword']+'</strong></td>';
                if(val['info_title']){
                    htmlStr += '<td>'+val['info_title']+'</td>';
                }else{
                    htmlStr += '<td>'+val['info_intro']+'</td>';
                }
                htmlStr += '<td><a onclick="selectMe(this)" href="javascript:void(0)" style="font-size:12px;" mid='+val['id']+'>选择</a></td></tr>';
            });
            $('tbody:last').html(htmlStr);
        },'json');
        
        $("#TB_overlayBG").css({display:"block",height:$(document).height()});
        $(".box").css({display:"block"});
    });
    //关闭弹框
    $(".close_pop").click(function(){
        $("#TB_overlayBG").css("display","none");
        $(".box ").css("display","none");
        location.reload();
    });
});

//选择
function selectMe(obj){
    $.post('mass_common.php',{act:'upMsg',msgid:$(obj).attr('mid'),type:type},function(res){
        if(res=='suc') location.reload();
    },'json');
}

//删除素材
function delRow(id){
    if(confirm('确定要删除此素材吗')){
        $.post('mass_common.php',{act:'del',msgid:id,type:type},function(res){
            if(res=='suc') location.reload();
        },'json');
    }
}

//发送 文字，单图文
function send(id,obj){
    if(confirm('确定要群发此素材吗？')){
        var layerLoad=layer.load(1,{shade:[0.1,'#666']});
        var actParam='send';
        if(type==3) actParam='sendMulti';
    
        $.post('mass_common.php',{act:actParam,msgid:id,type:type,groupid:$('#qunfa_group').val()},function(res){
            layer.close(layerLoad);
            if(res=='suc'){
                note_info('群发请求已发送成功',obj);
                setTimeout('window.location.reload()',1500);
            }else{
                note_info(res,obj,'warn');
            }
        },'json');
    }
}
</script>